﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using ForumsBrowser.Common;

namespace ForumsBrowser
{
    /// <summary>
    /// Interaction logic for MiniView.xaml
    /// </summary>
    public partial class MiniView : Window
    {
        public event EventHandler<ThreadSelectedEventArgs> ThreadSelected;
        private ThreadViewer _threadViewer = null;
        private ForumView _forumView = null;
        private ScrollBar scrollbar; 

        public MiniView()
        {
            InitializeComponent();
            openList = (Storyboard)Resources["openList"];
            closeList = (Storyboard)Resources["closeList"];
        }

        protected override void OnToolTipClosing(ToolTipEventArgs e)
        {
            e.Handled = true;
            base.OnToolTipClosing(e);
        }
       

        public bool FindScrollbar(DependencyObject obj)
        {
            if (obj is ScrollBar)
            {
                scrollbar = (ScrollBar)obj;
                return true;
            }
            else
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    bool result = FindScrollbar(VisualTreeHelper.GetChild(obj, i));
                    if (result) return result;
                }
            }
            return false;
        }

        public void SetForumView(ForumView view)
        {
            _forumView = view;
            this.mainGrid.Children.Clear();
            this.mainGrid.Children.Add(_forumView);
            FindScrollbar(mainGrid);
            _forumView.Detail = false;
            MiniViewMinHeight = (Configuration.MiniViewMinHeight * 59) + 22;
        }

        public ForumView RemoveForumView()
        {
            
            this.mainGrid.Children.Clear();
            _forumView.Detail = true;
            return _forumView;
        }

        private void forumView_ThreadSelected(object sender, ThreadSelectedEventArgs e)
        {
            var handler = ThreadSelected;
            if (handler != null)
                handler(sender, e);
          
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.miniWindow.DragMove();
        }

        public void FadeOut()
        {
            if (this.IsVisible && this.ActualHeight != Double.NaN)
            {
                closeList.Begin(this, true);
                ((Storyboard)Resources["fadeOut"]).Begin(scrollbar);
            }
        }
        private void miniWindow_MouseEnter(object sender, MouseEventArgs e)
        {
            if (this.IsVisible && this.ActualHeight != Double.NaN)
            {
                openList.Begin(this, true);
                ((Storyboard)Resources["fadeIn"]).Begin(scrollbar);
            }
        }

        private void miniWindow_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut();
        }

        private Storyboard closeList { get; set; }
        private Storyboard openList { get; set; }

        public static readonly DependencyProperty MiniViewMinHeightProperty =
            DependencyProperty.Register("MiniViewMinHeight", typeof(double), typeof(MiniView), new PropertyMetadata(79d));

        public double MiniViewMinHeight
        {
            get { return (double)this.GetValue(MiniViewMinHeightProperty); }
            set { this.SetValue(MiniViewMinHeightProperty, value); }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
